/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.bar.service.impl;

import java.util.Date;
import java.util.List;

import com.bar.model.Mailing;
import com.bar.service.MailingLocalServiceUtil;
import com.bar.service.base.MailingLocalServiceBaseImpl;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.dao.orm.Criterion;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

/**
 * The implementation of the mailing local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.bar.service.MailingLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author pgonzalm
 * @see com.bar.service.base.MailingLocalServiceBaseImpl
 * @see com.bar.service.MailingLocalServiceUtil
 */
public class MailingLocalServiceImpl extends MailingLocalServiceBaseImpl {
	
	private Log log = LogFactoryUtil.getLog(MailingLocalServiceBaseImpl.class);
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link com.bar.service.MailingLocalServiceUtil} to access the mailing local service.
	 */
	
	public void addMailingUser(String name, String surname, String email, Long phone, boolean mailing){
		try{
			if (name != null && email != null){
				
				DynamicQuery dQ = DynamicQueryFactoryUtil.forClass(Mailing.class);
				Criterion criterion = RestrictionsFactoryUtil.eq("email", email);
				dQ.add(criterion);
				
				List<Mailing> listaAux = MailingLocalServiceUtil.dynamicQuery(dQ);
				
				Mailing mailingBase = null;
				if (listaAux != null && !listaAux.isEmpty()){
					mailingBase = listaAux.get(0);
				}
				
				if (mailingBase == null){
					long publicidadId = CounterLocalServiceUtil.increment("Mailing");
					mailingBase = mailingPersistence.create(publicidadId);
				}
				
				mailingBase.setUserName(name);
				mailingBase.setUserSurname(surname);
				mailingBase.setEmail(email);
				mailingBase.setPhone(phone);
				mailingBase.setMailing(mailing);
				mailingBase.setCreateDate(new Date());
				
				MailingLocalServiceUtil.updateMailing(mailingBase);
			}
		} catch (SystemException e) {
			log.error(e);
		}
	}
}